#include <iostream>
#include <cstdio>
#include <pthread.h>
#include <unistd.h>
#include <vector>

void* routine(void* arg){
	sleep(1);
	std::string str = static_cast<const char*>(arg);
	delete (char*)arg;
	std::cout << str << std::endl;
	return nullptr;
}

int main(){
	std::vector<pthread_t*> tidarr;
	int num = 10;
	for(int i = 0; i < num; i++){
		pthread_t tid;
		char * id = new char[64];
		snprintf(id, 64, "创建的线程是 %d", i);
		int n = pthread_create(&tid, nullptr, routine, id);
		if(n == 0){
			tidarr.push_back(&tid);
		}
		else{
			continue;
		}
		sleep(1);
	}

	std::cout<<"主线程正在运行"<<std::endl;

	//循环等待
	for(int i = 0; i < num ; i++){
		printf("等待线程 %d 结束\n", i);
		pthread_join(*tidarr[i], nullptr);
		sleep(1);
	}

	std::cout<<"所有线程已经结束"<<std::endl;

	return 0;
}